package com.scm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.ProductMapper;
import com.scm.dao.SoitemMapper;
import com.scm.dao.SomainMapper;
import com.scm.entity.Soitem;
import com.scm.entity.Somain;
import com.scm.exception.TransactionException;
import com.scm.service.ISomainService;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SomainServiceImpl
        implements ISomainService
{
    @Autowired
    private SomainMapper somainMapper;
    @Autowired
    private SoitemMapper soitemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private HttpServletRequest req;

    @Transactional
    public void insert(Somain somain)
    {
        this.somainMapper.insert(somain);
        HashMap<String, Object> itemMap = new HashMap();
        itemMap.put("soId", somain.getSoId());
        itemMap.put("soitems", somain.getSoitems());
        int n = this.soitemMapper.insert(itemMap);
        if (n <= 0) {
            throw new TransactionException("销售单明细添加失败");
        }
        HashMap<String, Object> map = new HashMap();
        map.put("soitems", somain.getSoitems());
        map.put("type", Integer.valueOf(1));
        n = this.productMapper.updateSoNum(map);
        if (n <= 0) {
            throw new TransactionException("产品数量不足");
        }
    }

    @Transactional
    public void update(Somain somain)
    {
        int n = this.somainMapper.update(somain);
        if (n != 1) {
            throw new TransactionException("修改销售主单失败");
        }
        List<Soitem> items = this.soitemMapper.selectBySoId(somain.getSoId().longValue());
        if (items == null) {
            throw new TransactionException("没有查到销售单" + somain.getSoId() + "详情");
        }
        HashMap<String, Object> map = new HashMap();
        map.put("soitems", items);
        map.put("type", Integer.valueOf(2));
        n = this.productMapper.updateSoNum(map);
        if (n <= 0) {
            throw new TransactionException("产品数量不足");
        }
        n = this.soitemMapper.deleteBySoId(somain.getSoId().longValue());
        if (n == 0) {
            throw new TransactionException("删除销售明细单失败");
        }
        HashMap<String, Object> itemMap = new HashMap();
        itemMap.put("soId", somain.getSoId());
        itemMap.put("soitems", somain.getSoitems());
        n = this.soitemMapper.insert(itemMap);
        if (n <= 0) {
            throw new TransactionException("销售单明细修改失败");
        }
        itemMap.put("type", Integer.valueOf(1));
        n = this.productMapper.updateSoNum(itemMap);
        if (n <= 0) {
            throw new TransactionException("产品数量不足");
        }
    }

    @Transactional
    public void delete(long soid)
    {
        List<Soitem> items = this.soitemMapper.selectBySoId(soid);
        if (items == null) {
            throw new TransactionException("没有查到销售单" + soid + "详情");
        }
        HashMap<String, Object> map = new HashMap();
        map.put("soitems", items);
        map.put("type", Integer.valueOf(2));
        int n = this.productMapper.updateSoNum(map);
        if (n <= 0) {
            throw new TransactionException("修改产品销售待发数失败");
        }
        n = this.soitemMapper.deleteBySoId(soid);
        if (n == 0) {
            throw new TransactionException("删除销售单明细失败");
        }
        n = this.somainMapper.deleteBySoId(Long.valueOf(soid));
        if (n <= 0) {
            throw new TransactionException("删除销售单主信息失败");
        }
    }

    public PageInfo<Somain> selectByStatus(Integer page, int type, Integer payType)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        HashMap<String, Object> m = new HashMap();
        m.put("type", Integer.valueOf(type));
        m.put("payType", payType);
        if ((type == 1) || (type == 4))
        {
            String account = (String)this.req.getAttribute("login");
            m.put("account", account);
        }
        List<Somain> somains = this.somainMapper.selectSomain(m);
        System.out.println(somains);
        PageInfo<Somain> info = new PageInfo(somains);
        return info;
    }

    public PageInfo<Somain> selectReport(Integer page, String time)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        HashMap<String, Object> m = new HashMap();
        m.put("startDate", time + "-01");
        m.put("endDate", time + "-31");
        List<Somain> somains = this.somainMapper.selectCondition(m);
        PageInfo<Somain> info = new PageInfo(somains);
        return info;
    }
}
